import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/pages/login/login')
    },
    {
      path: '/home',
      name: 'Home',
      redirect: '/home/users',
      component: () => import('@/pages/Home/home'),
      children: [
        {
          path: 'users',
          name: 'users',
          component: () => import('@/pages/Home/users/users'),
        }
      ]
    },
    {
      path: '/detail',
      name: 'Detail',
      component: () => import('@/pages/detail/detail_index')
      // 懒加载 提高性能 跳转至当前页面时才加载当前组件
    },
    // {
    //   path: '*',
    //   redirect: '/login'
    // }
  ]
})
router.beforeEach((to, from, next) => {
  //  to 表示去到哪里
  //  from 从哪里来
  //  next 表示下一步需要做什么事情
  if (to.path == '/login') next()  // 写在同一行可以省略{}
  else {
    let tokens = window.sessionStorage.getItem('token')
    if (!tokens) {
      return next('/login')
    } else {
      next()

    }
  }
})
export default router